﻿

// actionFlag: 1 -> 新增；2 -> 修改；3 -> 删除
var actionFlag = getPar1("actionFlag");
var const_UserId = parent.parent._currentUserID;
var const_UserName = parent.parent._currentUserName;

$(document).ready(function () {
    var now = new Date();

    $("#Weight").numberbox({
        min: 0,
        precision: 2
    });

    $("#Price").numberbox({
        min: 0,
        precision: 2
    });

    $("#Qty").numberbox({
        min: 0,
        precision: 2
    });

    $("#Weight").bind("change", function () { checkProcessingWeight(); });
    $("#Price").bind("change", function () { checkProcessingPrice(); });
    $("#Qty").bind("change", function () { checkProcessingQty(); });
    // 新增
    if (actionFlag == 1) {
        obj = $("#SystemGenNo");
        genNewSequenceNo(obj, "WorkOrderNo");

        $("#AddUserName").val(const_UserName);
        $("#AddTime").val(fnGetMyDate(now));
        $("#WorkOrderNo").focus();
        $("#BOMNo").attr("disabled", "disabled");
        $("#Weight").val("0.00");
        $("#Price").val("0.00");
        $("#Price").change();
        $("#ReturnWeight").val("0.00");
        $("#PayAmount").val("0.00");
        $("#Qty").val("0.00");
        $("#ReturnQty").val("0.00");
    }
    else {
        getBOMList(instance[0].ItemNo);
    }

    // 修改
    if (actionFlag == 2) {
        $("#SystemGenNo").attr("disabled", "disabled");
        $("#WorkOrderNo").attr("disabled", "disabled");
        $("#Bu").attr("disabled", "disabled");
        $("#PlanDate").attr("disabled", "disabled");
        $("#ProductStartDate").attr("disabled", "disabled");
        $("#ProductEndDate").attr("disabled", "disabled");
        $("#ItemNo").attr("disabled", "disabled");
        $("#ProcessingFlag").attr("disabled", "disabled");
        $("#BOMNo").attr("disabled", "disabled");
    }
    $("#WorkOrderNo").bind("change", function () { var workOrderNo = $(this).val().toUpperCase(); fnCheckWorkOrderNo(workOrderNo); });
});

var customSetEvent = function () {
    if (actionFlag == "2") {
        parent.setAddNewButtonHiden();
        $("#Weight").val(parseFloat($("#Weight").val().toString()).toFixed(2).toString());
        $("#Price").val(parseFloat($("#Price").val().toString()).toFixed(2).toString());
        $("#Qty").val(parseFloat($("#Qty").val().toString()).toFixed(2).toString());
    }
}


// 检测生产指示编号是否重复
var fnCheckWorkOrderNo = function (val) {
    if (val != "") {
        var orderNo = fnGetData("V_BH_WorkOrder", "ID<>'" + instance[0].ID.toString() + "' and WorkOrderNo='" + val + "'");
        if (orderNo != null && orderNo != "") {
            alert("生产指示编号: [ " + val + " ] 已存在, 请重新输入!");
            setTimeout(function () {
                $("#WorkOrderNo").focus();
                $("#WorkOrderNo").val("");
            }, 10);
        }
    }
}

var getItemData = function (item) {
    return fnGetData("BH_ItemMaster", "ItemNo='" + item + "' or ItemModel='" + item + "' or ItemName='" + item + "'");
}

var setItemMaster = function () {
    //debugger;
    var itemNo = $("#ItemNo").attr("SValue");
    ItemMaster = getItemData(itemNo);

    if (ItemMaster == null || ItemMaster == "") {
        alert("物料：" + itemNo + " 还未定义!");
        //$("#ItemNo").val("");
        $("#ItemModel").val("");
        $("#ItemName").val("");
        $("#ItemNo").focus();
        return;
    }
    //debugger;
    //$("#ItemNo").val(ItemMaster.rows[0].ItemNo);
    $("#ItemModel").val(ItemMaster.rows[0].ItemModel);
    $("#ItemName").val(ItemMaster.rows[0].ItemName);
    $("#BOMNo").removeAttr("disabled");
    getBOMList(ItemMaster.rows[0].ItemNo);
}

/*
var getCompanyData = function (company) {
    return fnGetData("BH_Company", "CompanyNo='" + company + "' or CompanyName='" + company + "' or ShortName='" + company + "'");
}

// 设置供应商名称
var setCompanyName = function () {
    var companyNo = $("#Bu").attr("SValue");
    Company = getCompanyData(companyNo);

    if (Company == null || Company == "") {
        alert("供应商：" + companyNo + " 还未定义!");
        $("#CompanyName").val("");
        $("#Bu").focus();
        return;
    }

    $("#CompanyName").val(Company.rows[0].CompanyName);
}
*/

// 根据选中的半成品或成品加载相应的BOM
var getBOMList = function (itemNo) {
    //document.getElementById("BOMNo").options.length = 0;
    //debugger;
    $("#BOMNo").get(0).options.length = 0;
    bomList = fnGetData("V_BH_BOM", "ItemNo='" + itemNo + "'");
    if (bomList == null || bomList == "") {
        alert("当前半成品或成品不存在相应的BOM!");
        setTimeout(function () {
            $("#ItemNo").focus();
        }, 10);
        $("#BOMNo").attr("disabled", "disabled");
        return;
    }

    if (bomList.total > 0) {
        for (var i = 0; i < bomList.total; i++) {
            if (bomList.rows[i].ReferenceNo != undefined) {
                $("#BOMNo").append("<option value='" + bomList.rows[i].ReferenceNo + "'>" + bomList.rows[i].ReferenceNo + "</option>");
            }
        }
    }
    //debugger;
}

// 修改计划加工重量时检测
var checkProcessingWeight = function () {
    if (actionFlag == "2") {
        //debugger;
        var newWeight = parseFloat($("#Weight").val().toString()).toFixed(2);
        var oldWeight = parseFloat(instance[0].Weight).toFixed(2);
        var curReturnWeight = parseFloat($("#ReturnWeight").val().toString()).toFixed(2);

        if (newWeight < curReturnWeight) {
            alert("修改后的计划加工重量：" + newWeight.toString() + " 不能小于已加工重量: " + curReturnWeight.toString());
            setTimeout(function () {
                $("#Weight").focus();
                $("#Weight").val(oldWeight);
            }, 10);
            return;
        }
    }
    calcProcessingCost();
}

// 修改计划加工数量时检测
var checkProcessingQty = function () {
    if (actionFlag == "2") {
        //debugger;
        var newQty = parseFloat($("#Qty").val().toString()).toFixed(2);
        var oldQty = parseFloat(instance[0].Qty).toFixed(2);
        var curReturnQty = parseFloat($("#ReturnQty").val().toString()).toFixed(2);

        if (newQty < curReturnQty) {
            alert("修改后的计划加工数量：" + newQty.toString() + " 不能小于已加工数量: " + curReturnQty.toString());
            setTimeout(function () {
                $("#Qty").focus();
                $("#Qty").val(oldQty);
            }, 10);
            return;
        }
    }
}


// 修改加工单价时检测
var checkProcessingPrice = function () {
    if (actionFlag == "2") {
        if (parseFloat($("#ReturnWeight").val().toString()).toFixed(2) > 0) {
            alert("已加工重量大于0， 不允许修改加工单价!");
            setTimeout(function () {
                $("#Price").focus();
                $("#Price").val(parseFloat(instance[0].Price).toFixed(2).toString());
            }, 10);
            return;
        }
    }
    calcProcessingCost();
}

// 计算加工金额
var calcProcessingCost = function () {
    var processingPrice = parseFloat($("#Price").val());
    var weight = parseFloat($("#Weight").val());
    $("#Amount").val((processingPrice * weight).toFixed(2));
}


// 提交检测
var fnCustomCheck = function () {
    if (instance[0].WorkOrderNo == "" || instance[0].WorkOrderNo == null) {
        $("#WorkOrderNo").focus();
        return "生产指示编号不能为空!";
    }
    if (instance[0].Bu == "" || instance[0].Bu == null) {
        $("#Bu").focus();
        return "加工单位不能为空!";
    }
    if (instance[0].PlanDate == "" || instance[0].PlanDate == null) {
        $("#PlanDate").focus();
        return "计划日期不能为空!";
    }
    if (instance[0].ItemNo == "" || instance[0].ItemNo == null) {
        $("#ItemNo").focus();
        return "生产的半成品(成品)不能为空!";
    }
    if (instance[0].ProcessingFlag == "" || instance[0].ProcessingFlag == null) {
        $("#ProcessingFlag").focus();
        return "加工类型不能为空!";
    }
    if (instance[0].BOMNo == "" || instance[0].BOMNo == null) {
        $("#BOMNo").focus();
        return "BOM编号不能为空!";
    }
    if (instance[0].Weight == "" || instance[0].Weight == null) {
        $("#Weight").focus();
        return "计划生产重量不能为空!";
    }
    if (instance[0].Price == "" || instance[0].Price == null) {
        $("#Price").focus();
        return "加工费单价不能为空!";
    }
    return "";
}


var fnSetValue = function () {
    instance[0].WorkOrderNo = instance[0].WorkOrderNo.toUpperCase();
    instance[0].AddUser = const_UserId;
}

// 保存数据
var fnCustomSave = function () {
    fnSetValue();

    var dataAction = "";

    if (actionFlag == "1") {
        dataAction = "A";
    }
    else if (actionFlag == "2") {
        dataAction = "C";
    }
    else if (actionFlag == "3") {
        dataAction = "D";
    }
    //dataAction = (actionFlag == "1") ? "A" : "C";

    json = $.toJSON(instance[0]).replace(/'/g, "[B]").replace(/\+/g, "[J]");

    $.ajax({
        async: false, //异步请求
        cache: false, //不从浏览器缓存中加载
        type: "GET",
        /*dataType: "json",*/
        url: "/DCBH/DCBHCommonOperator.aspx",
        data: { Action: "procedure", entity: "V_BH_WorkOrder", json: json, detailJson: "", dataAction: dataAction },
        success: function (data) {
            //debugger;
            if (data != "") {
                if (data == "1") {
                    alert("数据保存成功!");

                    if (parent.SaveAndAddNewFlag == true) {
                        location.href = location.href
                    }

                    if (parent.SaveSuccessClose == true) {
                        parent.parent.CloseCurPage();
                    }
                }
                else {
                    alert(data);
                }
            }
        },
        error: function (e) {
            //debugger;
            alert("保存失败, 请联系开发人员，核查系统！");
            //alert(e.responseText);
            return;
        }
    });

} 